package org.example.threadlocal;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * file:TestThreadLocal.class
 * function:
 *
 * @author shaodan wang
 * @date 2024-06-06
 */

public class ThreadLocalTestDemo {

    private static final ThreadLocal<TianLuoClass> tianLuoThreadLocal = new ThreadLocal<>();
    public static void main(String[] args) throws InterruptedException {

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 5, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>());

        for (int i = 0; i < 1000; ++i) {
            threadPoolExecutor.execute(() -> {
                System.out.println("创建对象：");
                TianLuoClass tianLuoClass = new TianLuoClass();
                tianLuoThreadLocal.set(tianLuoClass);
                tianLuoClass = null; //将对象设置为 null，表示此对象不在使用了
                // tianLuoThreadLocal.remove();
            });
            Thread.sleep(10);
        }
    }

    static class TianLuoClass {
        // 100M
        private byte[] bytes = new byte[100 * 1024 * 1024];
    }
}

